package cn.lili.modules.wallet.serviceimpl;


import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.member.entity.vo.MemberFPointsHistoryVO;
import cn.lili.modules.member.entity.vo.MemberPointsHistoryVO;
import cn.lili.modules.payment.entity.dos.UserFPointsHistory;
import cn.lili.modules.payment.entity.dos.UserPointsHistory;
import cn.lili.modules.payment.entity.dos.UserWallet;
import cn.lili.modules.wallet.mapper.MemberFPointsHistoryMapper;
import cn.lili.modules.wallet.service.UserFPointsHistoryService;
import cn.lili.modules.wallet.service.WalletService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 用户积分历史业务层实现
 *
 * @author Bulbasaur
 * @since 2020-02-25 14:10:16
 */
@Service
@RequiredArgsConstructor
public class UserFPointsHistoryServiceImpl extends ServiceImpl<MemberFPointsHistoryMapper, UserFPointsHistory> implements UserFPointsHistoryService {


    private final WalletService walletService;

    @Override
    public MemberFPointsHistoryVO pointsHistoryVO(String userId) {
        //获取用户积分历史
        UserWallet userWallet = walletService.getFromUserid(userId);
        MemberFPointsHistoryVO memberFPointsHistoryVO = new MemberFPointsHistoryVO();
        if (ObjectUtil.isNull(userWallet)) {
            return memberFPointsHistoryVO;
        }
        memberFPointsHistoryVO.setFpoints(userWallet.getFmoney());
        memberFPointsHistoryVO.setTotalFPoint(userWallet.getTotalFmoney());
        return memberFPointsHistoryVO;
    }

    @Override
    public Page<UserFPointsHistory> pointsHistoryList(PageVO page, String userId, String username) {
        LambdaQueryWrapper<UserFPointsHistory> lambdaQueryWrapper = new LambdaQueryWrapper<UserFPointsHistory>()
                .eq(CharSequenceUtil.isNotEmpty(userId), UserFPointsHistory::getMemberId, userId)
                .like(CharSequenceUtil.isNotEmpty(username), UserFPointsHistory::getMemberName, username);
        //如果排序为空，则默认创建时间倒序
        if (CharSequenceUtil.isEmpty(page.getSort())) {
            page.setSort("createTime");
            page.setOrder("desc");
        }
        return this.page(PageUtil.initPage(page), lambdaQueryWrapper);
    }

}